#include "DeltaGlider.h"
#include "at1000.msh.resource.h"
#pragma warning(disable : 4996)

// --------------------------------------------------------------
// Read status from scenario file
// --------------------------------------------------------------
void DeltaGlider::clbkLoadStateEx (FILEHANDLE scn, void *vs)
{
    char *line;
//temporary initilizer delete later
	damage_partsRND[solar_panel1] = 0;
	damage_partsRND[solar_panel2] = 0;
	damage_partsRND[solar_panel3] = 0;
	damage_partsRND[solar_panel4] = 0;


	while (oapiReadScenario_nextline (scn, line)) {
        if (!strnicmp (line, "NOSECONE",                   8)) {sscanf (line+8, "%d%lf", &NOSE_CONE_STATUS, &nose_cone_proc);} 
		else if (!strnicmp (line, "GEAR",                  4)) {sscanf (line+4, "%d%lf", &gear_status, &gear_proc);} 
		else if (!strnicmp (line, "AIRLOCK_TUBE_STATUS",  19)) {sscanf (line+19, "%d%lf", &AIRLOCK_TUBE_STATUS, &air_lock_tube_proc);}
		else if (!strnicmp (line, "0AIRLOCK",               7)) {sscanf (line+7, "%d%lf", &OUTER_AIRLOCK_DOOR_STATUS, &outer_air_lock_proc);} 
		else if (!strnicmp (line, "IAIRLOCK",              8)) {sscanf (line+8, "%d%lf", &INNER_AIRLOCK_DOOR_STATUS, &inner_air_lock_proc);} 
		else if (!strnicmp (line, "AIRBRAKE",              8)) {sscanf (line+8, "%d%lf", &brake_status, &brake_proc);} 
		else if (!strnicmp (line, "RADIATOR",              8)) {sscanf (line+8, "%d%lf", &radiator_status, &radiator_proc);} 
		else if (!strnicmp (line, "LADDER",                6)) {sscanf (line+6, "%d%lf", &ladder_status, &ladder_proc);}
		else if (!strnicmp (line, "HATCH",                 5)) {sscanf (line+5, "%d%lf", &hatch_status, &hatch_proc);} 
		else if (!strnicmp (line, "TRIM",                  4)) {double trim; sscanf (line+4, "%lf", &trim); SetControlSurfaceLevel (AIRCTRL_ELEVATORTRIM, trim);}
		else if (!strnicmp (line, "TANKCONFIG",           10)) {sscanf (line+10, "%d", &tankconfig);} 
		else if (!strnicmp (line, "PARKING_BRAKE",        13)) {sscanf (line+13, "%d", &PARKING_BRAKE);}
		else if (!strnicmp (line, "SHIP_AGE",              8)) {sscanf (line+8, "%d%d%d", &ShipAge_seconds,&ShipAge_Min,&ShipAge_hours);}
		else if (!strnicmp (line, "Carbon_dioxide_level",  20)) {sscanf (line+20, "%.4f%.4f", &carbon_dioxide_level_001,&carbon_dioxide_level_002);}
		
		else if (!strnicmp (line, "CABINTEMP",        9)) {sscanf (line+9, "%0.1f", &CabinTemp);}
		
		
		else if (!strnicmp (line, "PSNGR", 5)) {
			DWORD i, res, pi[4];
			res = sscanf (line+5, "%d%d%d%d", pi+0, pi+1, pi+2, pi+3);
			for (i = 0; i < res; i++)
				if (pi[i]-1 < 4) psngr[pi[i]-1] = true;
		} else if (!strnicmp (line, "SKIN", 4)) {
			sscanf (line+4, "%s", skinpath);
			char fname[256];
			strcpy (fname, "DG\\Skins\\");
			strcat (fname, skinpath);
			int n = strlen(fname); fname[n++] = '\\';
			strcpy (fname+n, "dgmk4_1.dds");  skin[0] = oapiLoadTexture (fname);
			strcpy (fname+n, "dgmk4_2.dds");  skin[1] = oapiLoadTexture (fname);
			strcpy (fname+n, "idpanel1.dds"); skin[2] = oapiLoadTexture (fname);
			if (skin[2]) oapiBlt (insignia_tex, skin[2], 0, 0, 0, 0, 256, 256);
		} 
		else if (!strnicmp (line, "battery_charge_001", 14)) {
            double charges;
            charges = atof(line+14);
			set_vars(charges);


		}else if (!strnicmp (line, "DAMAGE_SOL1", 11)) {
				double temp = atof(line+11);
				set_damage(temp, solar_panel1);
		}else if (!strnicmp (line, "DAMAGE_SOL2", 11)) {
				double temp = atof(line+11);
				set_damage(temp, solar_panel2);
		}else if (!strnicmp (line, "DAMAGE_SOL3", 11)) {
				double temp = atof(line+11);
				set_damage(temp, solar_panel3);
		}
		else if (!strnicmp (line, "DAMAGE_SOL4", 11)) {
				double temp = atof(line+11);
				set_damage(temp, solar_panel4);
		}
		
		else if (!strnicmp (line, "POWER", 5)) {
			sscanf (line+5, "%d", &POWER);	
			
		}		
		// power systems 
		else if (!strnicmp (line, "TURBINE_FUEL_LEFT_POWER",        23)) {sscanf (line+23, "%d", &TURBINE_FUEL_LEFT_POWER);}		
		else if (!strnicmp (line, "TURBINE_IGNITION_LEFT_POWER",    27)) {sscanf (line+27, "%d", &TURBINE_IGNITION_LEFT_POWER);}		
		else if (!strnicmp (line, "TURBINE_FUEL_RIGHT_POWER",       24)) {sscanf (line+24, "%d", &TURBINE_FUEL_RIGHT_POWER);}		
		else if (!strnicmp (line, "TURBINE_IGNITION_RIGHT_POWER",   28)) {sscanf (line+28, "%d", &TURBINE_IGNITION_RIGHT_POWER);}		
		else if (!strnicmp (line, "MASTER_BAY_DOORS_POWER",         22)) {sscanf (line+22, "%d", &MASTER_BAY_DOORS_POWER);}		
		else if (!strnicmp (line, "BAY_DOOR_FORWARD_RIGHT_POWER",   28)) {sscanf (line+28, "%d", &BAY_DOOR_FORWARD_RIGHT_POWER);}
		else if (!strnicmp (line, "BAY_DOOR_AFT_RIGHT_POWER",       24)) {sscanf (line+24, "%d", &BAY_DOOR_AFT_RIGHT_POWER);}
		else if (!strnicmp (line, "BAY_DOOR_FORWARD_LEFT_POWER",    27)) {sscanf (line+27, "%d", &BAY_DOOR_FORWARD_LEFT_POWER);}
		else if (!strnicmp (line, "BAY_DOOR_AFT_LEFT_POWER",        23)) {sscanf (line+23, "%d", &BAY_DOOR_AFT_LEFT_POWER);}
		
		else if (!strnicmp (line, "AIRLOCK_MASTER_POWER",           20)) {sscanf (line+20, "%d", &AIRLOCK_MASTER_POWER);}
		else if (!strnicmp (line, "AIRLOCK_TUBE_POWER",             18)) {sscanf (line+18, "%d", &AIRLOCK_TUBE_POWER);}
		else if (!strnicmp (line, "INNER_AIR_LOCK_DOOR_POWER",      25)) {sscanf (line+25, "%d", &INNER_AIR_LOCK_DOOR_POWER);}
        else if (!strnicmp (line, "OUTER_AIR_LOCK_DOOR_POWER",      25)) {sscanf (line+25, "%d", &OUTER_AIR_LOCK_DOOR_POWER);}
		else if (!strnicmp (line, "NOSE_CONE_POWER",                15)) {sscanf (line+15, "%d", &NOSE_CONE_POWER);}
		else if (!strnicmp (line, "CHAMBER_POWER",                  13)) {sscanf (line+13, "%d", &CHAMBER_POWER);}
		else if (!strnicmp (line, "DOCK_RELEASE_POWER",             18)) {sscanf (line+18, "%d", &DOCK_RELEASE_POWER);}




		
		else if(Crew.LoadAllMembersFromOrbiterScenario(line)==TRUE){ 
		
				//stuff
		
		
		}else {
            ParseScenarioLineEx (line, vs);
			// unrecognised option - pass to Orbiter's generic parser
        }
    }

	// modify tank configuration (DG-S only)
	if (tankconfig != 0) {
		switch (tankconfig) {
		case 1:
			max_rocketfuel = TANK1_CAPACITY;
			max_scramfuel  = TANK2_CAPACITY;
			break;
		case 2:
			max_rocketfuel = TANK2_CAPACITY;
			max_scramfuel  = TANK1_CAPACITY;
			break;
		}
		SetPropellantMaxMass (ph_main, max_rocketfuel);
		SetPropellantMaxMass (ph_scram, max_scramfuel);
	}
	

}


// --------------------------------------------------------------
// Write status to scenario file
// --------------------------------------------------------------
void DeltaGlider::clbkSaveState (FILEHANDLE scn)
{
	char cbuf[256];
	int i;

	// Write default vessel parameters
	VESSEL2::clbkSaveState (scn);

	// Write custom parameters
	if (gear_status)               {sprintf (cbuf, "%d %0.4f", gear_status, gear_proc); oapiWriteScenario_string (scn, "GEAR", cbuf);}
	if (NOSE_CONE_STATUS)          {sprintf (cbuf, "%d %0.4f", NOSE_CONE_STATUS, nose_cone_proc); oapiWriteScenario_string (scn, "NOSECONE", cbuf);}
	if (AIRLOCK_TUBE_STATUS)       {sprintf (cbuf, "%d %0.4f", AIRLOCK_TUBE_STATUS, air_lock_tube_proc); oapiWriteScenario_string (scn, "AIRLOCK_TUBE_STATUS", cbuf);}
	if (OUTER_AIRLOCK_DOOR_STATUS) {sprintf (cbuf, "%d %0.4f", OUTER_AIRLOCK_DOOR_STATUS, outer_air_lock_proc); oapiWriteScenario_string (scn, "OAIRLOCK", cbuf);}
	if (INNER_AIRLOCK_DOOR_STATUS) {sprintf (cbuf, "%d %0.4f", INNER_AIRLOCK_DOOR_STATUS, inner_air_lock_proc);	oapiWriteScenario_string (scn, "IAIRLOCK", cbuf);}
	if (brake_status)              {sprintf (cbuf, "%d %0.4f", brake_status, brake_proc); oapiWriteScenario_string (scn, "AIRBRAKE", cbuf);}
	if (radiator_status)           {sprintf (cbuf, "%d %0.4f", radiator_status, radiator_proc); oapiWriteScenario_string (scn, "RADIATOR", cbuf);}
	if (ladder_status)             {sprintf (cbuf, "%d %0.4f", ladder_status, ladder_proc); oapiWriteScenario_string (scn, "LADDER", cbuf);}
	if (hatch_status)              {sprintf (cbuf, "%d %0.4lf", hatch_status, hatch_proc); oapiWriteScenario_string (scn, "HATCH", cbuf);}
	if(PARKING_BRAKE)              {sprintf (cbuf, "%d ",  PARKING_BRAKE); oapiWriteScenario_string (scn, "PARKING_BRAKE", cbuf);}
	                               {sprintf (cbuf, "%d %d %d", ShipAge_seconds, ShipAge_Min,ShipAge_hours); oapiWriteScenario_string (scn, "SHIP_AGE", cbuf);}
	                               {sprintf (cbuf, "%f %f", carbon_dioxide_level_001, carbon_dioxide_level_002); oapiWriteScenario_string (scn, "Carbon_dioxide_level", cbuf);}
	                                
								   {sprintf (cbuf, "%0.1f", CabinTemp); oapiWriteScenario_string (scn, "CABINTEMP", cbuf);}
	
	
	for (i = 0; i < 4; i++)
		if (psngr[i]) {
			sprintf (cbuf, "%d", i+1);
			for (++i; i < 4; i++)
				if (psngr[i]) sprintf (cbuf+strlen(cbuf), " %d", i+1);
			oapiWriteScenario_string (scn, "PSNGR", cbuf);
			break;
		}
	if (skinpath[0])
		oapiWriteScenario_string (scn, "SKIN", skinpath);
	for (i = 0; i < 7; i++)
		if (beacon[i].active) {
			sprintf (cbuf, "%d %d %d", beacon[0].active, beacon[3].active, beacon[5].active);
			oapiWriteScenario_string (scn, "LIGHTS", cbuf);
			break;
		}

		double trim = GetControlSurfaceLevel (AIRCTRL_ELEVATORTRIM);
	if (trim) oapiWriteScenario_float (scn, "TRIM", trim);

	if (tankconfig)
		oapiWriteScenario_int (scn, "TANKCONFIG", tankconfig);
	if (battery_charge_001) {
		sprintf (cbuf, "%0.5f", battery_charge_001);
		oapiWriteScenario_string (scn, "battery_charge_001", cbuf);}
	if (damaged_parts) {
		sprintf (cbuf, "%d%d", damaged_parts[solar_panel1],damage_partsRND[solar_panel1]);
		oapiWriteScenario_string (scn, "DAMAGE_SOL1", cbuf);}
	if (damaged_parts) {
		sprintf (cbuf, "%d%d", damaged_parts[solar_panel2],damage_partsRND[solar_panel2]);
		oapiWriteScenario_string (scn, "DAMAGE_SOL2", cbuf);}
	if (damaged_parts) {
		sprintf (cbuf, "%d%d", damaged_parts[solar_panel3],damage_partsRND[solar_panel3]);
		oapiWriteScenario_string (scn, "DAMAGE_SOL3", cbuf);}
	if (damaged_parts) {
		sprintf (cbuf, "%d%d", damaged_parts[solar_panel4],damage_partsRND[solar_panel4]);
		oapiWriteScenario_string (scn, "DAMAGE_SOL4", cbuf);}

     // power systems 
    if(POWER)	                       {sprintf (cbuf, "%d ",  POWER); oapiWriteScenario_string (scn, "POWER", cbuf);}
    if(TURBINE_FUEL_LEFT_POWER)        {sprintf (cbuf, "%d ",  TURBINE_FUEL_LEFT_POWER); oapiWriteScenario_string (scn, "TURBINE_FUEL_LEFT_POWER", cbuf);}
    if(TURBINE_IGNITION_LEFT_POWER)	   {sprintf (cbuf, "%d ",  TURBINE_IGNITION_LEFT_POWER); oapiWriteScenario_string (scn, "TURBINE_IGNITION_LEFT_POWER", cbuf);}
	if(TURBINE_FUEL_RIGHT_POWER)	   {sprintf (cbuf, "%d ",  TURBINE_FUEL_RIGHT_POWER); oapiWriteScenario_string (scn, "TURBINE_FUEL_RIGHT_POWER", cbuf);}
    if(TURBINE_IGNITION_RIGHT_POWER)   {sprintf (cbuf, "%d ",  TURBINE_IGNITION_RIGHT_POWER); oapiWriteScenario_string (scn, "TURBINE_IGNITION_RIGHT_POWER", cbuf);}
   	if(MASTER_BAY_DOORS_POWER)         {sprintf (cbuf, "%d ",  MASTER_BAY_DOORS_POWER); oapiWriteScenario_string (scn, "MASTER_BAY_DOORS_POWER", cbuf);}
	if(BAY_DOOR_FORWARD_RIGHT_POWER)   {sprintf (cbuf, "%d ",  BAY_DOOR_FORWARD_RIGHT_POWER); oapiWriteScenario_string (scn, "BAY_DOOR_FORWARD_RIGHT_POWER", cbuf);}
    if(BAY_DOOR_AFT_RIGHT_POWER)       {sprintf (cbuf, "%d ",  BAY_DOOR_AFT_RIGHT_POWER); oapiWriteScenario_string (scn, "BAY_DOOR_AFT_RIGHT_POWER", cbuf);}
    if(BAY_DOOR_FORWARD_LEFT_POWER)    {sprintf (cbuf, "%d ",  BAY_DOOR_FORWARD_LEFT_POWER); oapiWriteScenario_string (scn, "BAY_DOOR_FORWARD_LEFT_POWER", cbuf);}
    if(BAY_DOOR_AFT_LEFT_POWER)        {sprintf (cbuf, "%d ",  BAY_DOOR_AFT_LEFT_POWER); oapiWriteScenario_string (scn, "BAY_DOOR_AFT_LEFT_POWER", cbuf);}

    if(AIRLOCK_MASTER_POWER)           {sprintf (cbuf, "%d ",  AIRLOCK_MASTER_POWER); oapiWriteScenario_string (scn, "AIRLOCK_MASTER_POWER", cbuf);}
    if(AIRLOCK_TUBE_POWER)             {sprintf (cbuf, "%d ",  AIRLOCK_TUBE_POWER); oapiWriteScenario_string (scn, "AIRLOCK_TUBE_POWER", cbuf);}
    if(INNER_AIR_LOCK_DOOR_POWER)      {sprintf (cbuf, "%d ",  INNER_AIR_LOCK_DOOR_POWER); oapiWriteScenario_string (scn, "INNER_AIR_LOCK_DOOR_POWER", cbuf);}
    if(OUTER_AIR_LOCK_DOOR_POWER)      {sprintf (cbuf, "%d ",  OUTER_AIR_LOCK_DOOR_POWER); oapiWriteScenario_string (scn, "OUTER_AIR_LOCK_DOOR_POWER", cbuf);}
    if(NOSE_CONE_POWER)                 {sprintf (cbuf, "%d ",  NOSE_CONE_POWER); oapiWriteScenario_string (scn, "NOSE_CONE_POWER", cbuf);}
    if(CHAMBER_POWER)                  {sprintf (cbuf, "%d ",  CHAMBER_POWER); oapiWriteScenario_string (scn, "CHAMBER_POWER", cbuf);}
    if(DOCK_RELEASE_POWER)             {sprintf (cbuf, "%d ",  DOCK_RELEASE_POWER); oapiWriteScenario_string (scn, "DOCK_RELEASE_POWER", cbuf);}


	Crew.SaveAllMembersInOrbiterScenarios(scn);

}
